$(function () {
    if (localStorage.getItem('user')) {
        window.location.replace('/setting')
    }
    $('#login').on('tap', () => {
        let username = $.trim($('#username').val())
        let password = $.trim($('#password').val())
        $.ajax({
            type: 'POST',
            url: '/user/login',
            dataType: 'json',
            data: {
                username, password
            },
            beforeSend: () => {
                $('#login').html('登陆中...')
                $('#login').attr('disabled', 'disabled')
            },
            success: (result) => {
                let timer = setTimeout(() => {
                    if (result.error) {
                        mui.toast(result.message);
                        $('#login').html('登陆')
                        $('#login').removeAttr('disabled')
                        return
                    } else if (result.success) {
                        mui.alert('登陆成功!', () => {
                            console.log(result.user)
                            localStorage.setItem('user', JSON.stringify(result.user))
                            window.location.replace('/setting')
                        });
                    } else {
                        mui.alert('无法访问服务,请联系wx:13278865643');
                    }
                }, 1000)

            }
        })
    })
})